package com.kma.summer2012.client.view.viewmap;

import com.google.gwt.dom.client.TableCellElement;
import com.google.gwt.dom.client.TableRowElement;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.ui.DecoratedPopupPanel;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Label;
import com.kma.summer2012.client.styles.style2.Recources;

import java.util.Map;

public class MyTable extends FlexTable {
    private DecoratedPopupPanel simplePopup = new DecoratedPopupPanel(true);


    public MyTable() {
        sinkEvents(Event.ONMOUSEOVER | Event.ONMOUSEOUT);
        simplePopup.setStyleName(Recources.INSTANCE.styles().popup());

    }

    private Map<Integer, String> requestInfoMap;

    public void setRequestInfoMap(Map<Integer, String> requestInfoMap) {
        this.requestInfoMap = requestInfoMap;
    }

    @Override
    public void onBrowserEvent(Event event) {
        super.onBrowserEvent(event);
        Element td = getEventTargetCell(event);

        if (td == null) {
            return;
        }

        String textInCell = td.getInnerText();
        int column = TableCellElement.as(td).getCellIndex();

        switch (DOM.eventGetType(event)) {
            case Event.ONMOUSEOVER: {

                if (column == 0 && textInCell.charAt(0) == 'R') {
                    int request_id = Integer.parseInt(textInCell.substring(textInCell.indexOf('№') + 1));
                    System.out.println(request_id);
                    int left = td.getAbsoluteLeft() - 10;
                    int top = td.getAbsoluteTop() - 10;
                    simplePopup.setPopupPosition(left, top);
                    System.out.println("requestInfoForPopup" + requestInfoMap.get(request_id));
                    simplePopup.setWidget(new Label(requestInfoMap.get(request_id)));
                    simplePopup.show();
                }
                break;
            }
            case Event.ONMOUSEOUT: {
                simplePopup.hide();
                break;
            }
        }
    }
}